/**
 * Created by moko1988 on 15/12/1.
 */
define([
    'backbone',
    'models/feedback/list',
    'text!templets/feedback/list.html',
    'text!templets/feedback/category-modal-add.html',
    'utils/dataTableUtil',
    'utils/urlUtil',
    'jquerypage',
    'select2',
    'moment'
], function(
    BackBone,
    Model,
    Tpl,
    ModalAdd,
    DataTable,
    UrlUtil
) {
    var view = Backbone.View.extend({
        el: '#CONTENT',
        events: {
            'click [role=check]': 'editFun', //详情
            'change #pageSize': 'changePageSize', //选择分页宽度
            'click [role=rank]': 'rank', //排序
            'click [role=review]': 'review', //审核
            'click [role=recommend]': 'recommend', //推荐
            'click #checkAll': 'checkAll', //全选、取消
            'click input[name=idarr]': 'checkItem', //选择、取消单条记录
            'change #list-category': 'changeListCategory'
        },
        /*初始化*/
        initialize: function() {
            $(this.el).html(Tpl);
            $(this.el).off();
            this.page = '1';
            this.listRows = '10';
            this.idArr = [];
            this.categoryid = '';
            $('#pageSize').select2();
            this.renderCategory();
        },
        renderCategory: function(){
            this.render();
            
        },
        changeListCategory: function(e){
            var $target = $(e.currentTarget),
                val = $target.val();
            this.categoryid = val;
            this.render();
        },
        /*全选、取消*/
        checkAll: function(e) {
            var $target = $(e.currentTarget),
                isCheck = $target.prop('checked'),
                _this = this;
            if (isCheck) {
                $('input[name=idarr]').prop('checked', true);
                $('input[name=idarr]').each(function() {
                    _this.idArr.push($(this).val());
                })
            } else {
                $('input[name=idarr]').prop('checked', false);
                this.idArr = [];
            }
        },
        /*选择、取消单条记录*/
        checkItem: function() {
            this.idArr = [];
            var isCheckAll = true,
                _this = this;
            $('input[name=idarr]').each(function() {
                if ($(this).prop('checked')) {
                    _this.idArr.push($(this).val());
                } else {
                    isCheckAll = false;
                }
            })
            if (isCheckAll) {
                $('#checkAll').prop('checked', true);
            } else {
                $('#checkAll').prop('checked', false);
            }
        },
        /*选择分页宽度*/
        changePageSize: function(e) {
            var $target = $(e.currentTarget),
                val = $target.val();
            this.listRows = val;
            this.render();
        },
        /*渲染结果列表*/
        render: function() {
            $('#chart_table_div').html(loading);
            var _this = this;
            Model.fetchList({
                type: 'post',
                data: {
                    page: this.page,
                    page_size: this.listRows,
                },
                success: function(map) {
                    var data = map.list,
                        page = map.page,
                        count = data.length;
                    _this.pageTotal = page.total_page;
                    _.each(data,function(o, i){

                         o.checkbox = '<input type="checkbox" value="'+ o.id +'" name="idarr">';
                        

                    })

                  var tableHead = ['<input type="checkbox" id="checkAll">', '序号', '建议类型', '联系人', '联系电话' , '微信号' , 'QQ号'],
                        tableBody = _.zip(
                            _.pluck(data,'checkbox'),
                            _.pluck(data,'id'),
                            _.pluck(data,'type_name'),
                            _.pluck(data,'name'),
                            _.pluck(data,'phone'),
                            _.pluck(data,'weixin'),
                            _.pluck(data,'qq')
                        );
                    DataTable.drawTable({
                        '$el': $('#chart_table_div'),
                        'thead': tableHead,
                        'tbody': tableBody
                    });

                    $("#pager").pager({
                        pagenumber: _this.page,
                        pagecount: _this.pageTotal,
                        buttonClickCallback: function(page){
                            _this.pageClick(page);
                        }
                    });
                }
            })
        },
        /*分页*/
        pageClick: function(page) {
            var _this = this;
            this.page = page;
            $("#pager").pager({
                pagenumber: this.page,
                pagecount: this.pageTotal,
                buttonClickCallback: function(page){
                    _this.pageClick(page);
                }
            });
            this.render();
        },
        addFun: function() {
            window.location.hash = 'view/article-add'
        },

        /*查看详情*/
       editFun: function(e) {
            $(this.el).append(ModalAdd);
            var $target = $(e.currentTarget),
                $myModal = $('#myModal'),
                id = this.idArr[0],
                // id = $target.attr('data-id'),
                $form = $myModal.find('form[role=form]');

            if (this.idArr.length) { 
                $myModal.modal();
                $myModal.find('h4').html('反馈详情');
                $myModal.find('[role=save]').prop('disabled', true);
                /*弹窗关闭*/
                $myModal.on('hidden.bs.modal', function(e) {
                    $myModal.remove();
                });
                $('#loading').html(loading);
                $('#data-form').hide();
                Model.fetchInfo({
                    type: 'post',
                    data: {
                        id: id
                    },
                    success: function(map) {                 
                        $myModal.find('input[name=id]').val(id);
                        $myModal.find('textarea[name=details]').val(map.content);
                        $('#loading').html('');
                        $('#data-form').show();
                        $myModal.find('[role=save]').prop('disabled', false);
                    }
                })
             } else {
                jAlert('请选择要操作的数据');
            }
        },
       
    })
    return view;
})